package com.future.controller;

import com.future.po.Driver;
import com.future.service.DriverService;
import com.future.vo.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author zlb
 * @date 2022/4/26 10:07
 */
@RestController
@RequestMapping("/driver")
public class DriverController {

    @Resource
    private DriverService driverService;

    @GetMapping("/list")
    public Result<List<Driver>> list(){
        List<Driver> list = driverService.list();
        Result<List<Driver>> result = new Result<>();
        if (list == null || list.isEmpty()){
            result.setMessage("未找到相关信息！");
            return result;
        }
        result.setSuccess(true);
        result.setMessage("查询成功！");
        result.setData(list);
        return result;
    }

    @GetMapping("/get")
    public Result<Driver> getDriver(@Param("id") Integer id){
        Result<Driver> result = new Result<>();
        Driver driver = driverService.getById(id);
        if (driver == null){
            result.setSuccess(true);
            result.setMessage("未找到相关信息！");
            return result;
        }
        result.setMessage("查询成功！");
        result.setData(driver);
        return result;
    }

    @PostMapping("/update")
    public Result<Integer> getDriver(Driver driver){
        Result<Integer> result = new Result<>();
        boolean flag = driverService.updateById(driver);
        result.setSuccess(flag);
        if (flag){
            result.setMessage("更新成功！");
            return result;
        }
        result.setMessage("更新失败！");
        return result;
    }

    @PostMapping("/add")
    public Result<Integer> add(Driver driver){
        Result<Integer> result = new Result<>();
        boolean flag = driverService.save(driver);
        result.setSuccess(flag);
        if (flag){
            result.setMessage("新增成功！");
            return result;
        }
        result.setMessage("新增失败！");
        return result;
    }

    @PostMapping("/del")
    public Result<Boolean> add(Integer id){
        Result<Boolean> result = new Result<>();
        boolean flag = driverService.removeById(id);
        result.setSuccess(flag);
        if (flag){
            result.setMessage("删除成功！");
            return result;
        }
        result.setMessage("删除失败！");
        return result;
    }
}
